#include <CppLinuxSerial/SerialPort.hpp>
#include <unistd.h>
using namespace mn::CppLinuxSerial;

int main() {

	#if 0
	// Create serial port object and open serial port at 57600 buad, 8 data bits, no parity bit, and one stop bit (8n1)
	SerialPort serialPort("/dev/ttyS1", BaudRate::B_57600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
	// Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
	serialPort.SetTimeout(-1); // Block when reading until any data is received
	serialPort.Open();

	// Write some ASCII data
	serialPort.Write("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");


	// Read some data back (will block until at least 1 byte is received due to the SetTimeout(-1) call above)
	//std::string readData;
	//serialPort.Read(readData);
    //std::cout<< "Read " << readData << std::endl;
	#else
	// Create serial port object and open serial port at 57600 buad, 8 data bits, no parity bit, and one stop bit (8n1)
	SerialPort serialPort("/dev/ttyS1", BaudRate::B_115200, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
	// Use SerialPort serialPort("/dev/ttyACM0", 13000); instead if you want to provide a custom baud rate
	serialPort.SetTimeout(1000); // Block when reading until any data is received
	serialPort.Open();


    std::cout<< "before sleep...." << std::endl;
    sleep(5);
    std::cout<< "after sleep...." << std::endl;

    int n = 0;
    while (1)
    {
    	std::string readData;
		serialPort.Read(readData);
		size_t len = readData.length();
    	if (0 == len)
		{
			std::cout<< "total len = " << n << std::endl;
			n = 0;
		}
		else
		{
			n += len;
		}
    }
	#endif

	// Close the serial port
	serialPort.Close();
}